package com.example.pma.controller;
import com.example.pma.project.entity.Project;
import com.example.pma.project.service.ProjectService;
import com.example.pma.student.entity.Student;
import com.example.pma.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("projects")
public class ProjectController {

    @Autowired
    ProjectService projectService;

    @Autowired
    StudentService studentService;

 @GetMapping
 public String displayProjects(Model model){
     List<Project> projects = projectService.getAll();
     model.addAttribute("projects",projects);
     return "projects/list-projects";
 }

 @GetMapping("/new")
 public String displayProjectForm(Model model){

     Project aProject = new Project();
     List<Student> students = studentService.getAll();
     model.addAttribute("project",aProject);
     model.addAttribute("allStudents", students);
     return "projects/new-project";
 }

 @PostMapping("/save")
 public String createProject(Model model, @RequestParam List<Long> students, Project project){

     projectService.save(project);

     return "redirect:/projects";
 }
}
